package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.IDishFlavorService;
import com.itheima.reggie.service.IDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {


    @Autowired

    private IDishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品数据
        this.save(dishDto);

        Long dishId = dishDto.getId();

        //确定是否有口味


        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }


    @Override
    public void updateWithFlavor(DishDto dishDto) {

        //更新菜品数据
        this.updateById(dishDto);

        //更改dishFlavor数据 remove+add

        //添加条件来remove和save
        LambdaQueryWrapper<DishFlavor> lqw = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);


    }


    @Override
    public DishDto selectById(Long id) {

        //获取dish
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //将dish存到dishDto
        BeanUtils.copyProperties(dish, dishDto);

        //创建条件
        LambdaQueryWrapper<DishFlavor> lqw = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, id);
        //按dishId找到dishFlavor并存入dishDto
        List<DishFlavor> flavors = dishFlavorService.list(lqw);


        dishDto.setFlavors(flavors);


        return dishDto;
    }

    @Override
    public void deleteByIds(List<Long> ids) {

//        //添加条件
//        LambdaQueryWrapper<Dish> lqw = Wrappers.lambdaQuery(Dish.class)
//                .in(Dish::getId,ids);

        this.removeByIds(ids);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(Dish::getStatus,status).in(Dish::getId,ids);

        this.update(updateWrapper);

    }

    @Override
    public List<Dish> selectWithCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> lqw=Wrappers.lambdaQuery(Dish.class).eq(Dish::getCategoryId,categoryId);
        return this.list(lqw);
    }


}
